#! /usr/bin/env python
# -*- coding: utf-8 -*-

# *************************************************************
#     Filename @  get_last_commit.py
#       Author @  Huoty
#  Create date @  2016-09-03 12:40:05
#  Description @  
# *************************************************************

import os

def get_last_commit(pro_path):
    class UnfoundError(Exception): pass

    def find_exe(exe):
        for path in os.environ['PATH'].split(':'):
            if path and os.path.exists(os.path.join(path, exe)):
                return os.path.join(path, exe)
        raise UnfoundError("executable %s file not found" % exe)
        
    git = find_exe("git") + " log --oneline"
    head = find_exe("head") + " -n 1"
    awk = find_exe("awk") + " -F ' ' '{print $1}'"
    
    cmd = " | ".join([git, head, awk])
    print cmd

    curr_path = os.getcwd()
    os.chdir(pro_path)
    commit = os.popen(cmd).read().strip()
    os.chdir(curr_path)

    return commit
    

# Script starts from here

if __name__ == "__main__":
    print get_last_commit(os.path.join(os.environ["HOME"], "aboutme/pythonstudy"))

